<template>
	<view class="page">
		<!-- 地址 -->
		<view class="address-data">
			<view class="address-list" @click="onSkip('address')">

				<view class="list">
					<text>{{address.region}}</text>
				</view>
				<view class="list">
					<text class="address">{{address.address}}</text>
				</view>
				<view class="list">
					<text>{{address.name}}</text>
					<text>{{address.phone}}</text>
				</view>
				<view class="list">
					<text class="tips">(如果快递不方便接收，您可以选择暂时寄存服务)</text>
				</view>
			</view>
			<view class="bar">

			</view>
		</view>
		<!-- 商品 -->
		<view class="goods-data">
			<view class="goods-title">
				<text>商品信息</text>
			</view>
			<view class="goods-list">
				<!-- 	v-for="(shop,index) in shopping" :key="index" -->
				<view class="list" v-for="(shop,index) in test">
					<view class="thumb">
						<image :src="'/static/img/goods_thumb_0'+(index+1)+'.png'" mode=""></image>
					</view>
					<view class="item">
						<view class="title">
							<text class="name one-omit">{{shop.goodsName}}</text>
							<text class="attr">{{shop.norm}}</text>
						</view>
						<view class="price-number">
							<view class="price">
								<text class="min">￥</text>
								<text class="max">{{shop.price}}</text>
							</view>
							<view class="number">
								<text>x {{shop.quantity}}</text>
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class="delivery">
				<div class="list">
					<view class="title">配送</view>
					<view class="content">
						<text>快递运输</text>
						<text class="iconfont icon-more"></text>
					</view>
				</div>
				<!-- <div class="list">
					<view class="title">运费险</view>
					<view class="content">
						<text>￥10.00</text>
						<text class="iconfont icon-check"></text>
					</view>
				</div> -->
				<div class="list">
					<view class="title">留言</view>
					<view class="content">
						<input type="text" placeholder="选填,建议先和商家沟通确认" v-model="message">
					</view>
				</div>
			</view>
		</view>
		<!-- 优惠 -->
		<view class="discounts-data">
			<view class="discounts">
				<div class="list" @click="$refs['InvoiceInfo'].show()">
					<view class="title">发票</view>
					<view class="content">
						<text>不开发票</text>
						<text class="iconfont icon-more"></text>
					</view>
				</div>
				<div class="list" @click="$refs['UseCoupon'].show()">
					<view class="title">优惠券</view>
					<view class="content">
						<text>无可用</text>
						<text class="iconfont icon-more"></text>
					</view>
				</div>
				<div class="list">
					<view class="title">积分</view>
					<view class="content">
						<text>共300，满1000可用</text>
						<!-- <text class="iconfont icon-more"></text> -->
					</view>
				</div>
			</view>
		</view>
		<!-- 订单金额 -->
		<view class="order-price">
			<view class="price-list">
				<view class="list">
					<view class="title">
						<text>商品金额</text>
					</view>
					<view class="price">
						<text>￥{{sum.sum}}</text>
					</view>
				</view>
				<view class="list">
					<view class="title">
						<text>会员折扣</text>
					</view>
					<view class="price">
						<text>-￥0</text>
					</view>
				</view>
				<view class="list">
					<view class="title">
						<text>运费</text>
					</view>
					<view class="price">
						<text class="highlight">+￥{{sum.yunsum}}</text>
					</view>
				</view>
				<view class="list">
					<view class="title">
						<text>运费险</text>
					</view>
					<view class="price">
						<text class="highlight">+￥0.00</text>
					</view>
				</view>
			</view>
		</view>
		<!-- 地址提示 -->
		<view class="address-tips" :style="scrollTop >= 100 ? '':'display:none'">
			<text>{{address.region}}{{address.address}}</text>
		</view>
		<!-- 底部合计提交 -->
		<view class="footer-submit">
			<view class="price">
				<text class="min">￥</text>
				<text class="max">{{sum.zong}}</text>
			</view>
			<view class="submit" @click="onSubmit">
				<text>提交订单</text>
			</view>
		</view>
		<!-- 发票 -->
		<invoice-info ref="InvoiceInfo"></invoice-info>
		<!-- 优惠券 -->
		<use-coupon ref="UseCoupon"></use-coupon>
	</view>
</template>

<script>
	import InvoiceInfo from '../../components/InvoiceInfo/InvoiceInfo.vue';
	import UseCoupon from '../../components/UseCoupon/UseCoupon.vue';
	import axios from 'axios';
	export default {
		components: {
			// 发票
			InvoiceInfo,
			// 优惠券
			UseCoupon,
		},
		data() {
			return {
				cartID: this.$queue.getData("gooda"),
				test: [],
				scrollTop: 0,
				address: {},
				addId: this.$route.query.addId,
				sum: {},
				message: '',

			};
		},
		created() {
			axios.request("http://localhost:8089/order/findshopping/" + this.cartID).then(
				res => {
					this.test = res.data
					console.log(this.test)
					this.sum = this.$options.methods.addnum(this.test);
					console.log(this.sum)
					// this.zong = (this.sum.sum)+(this.sum.yunsum)-(this.sum.youhui)
					// console.log(this.zong)
				}
			)
			// axios.request().then()
			axios.request("http://localhost:8089/order/add/1").then(
				res => {
					this.address = res.data
				}
			)
			if (this.addId != undefined) {
				axios.request("http://localhost:8089/order/addById/" + this.addId).then(
					res => {
						this.address = res.data
					}
				)
			}

		},
		onPageScroll(e) {
			this.scrollTop = e.scrollTop;
		},
		methods: {
			/**
			 * 提交订单
			 */
			onSubmit() {
				axios.post("http://localhost:8089/order/insertorder", {
					userId: this.$route.query.addId,
					amount: this.sum.sum,
					freightPrice: this.sum.yunsum,
					couponPrice: this.sum.youhui,
					actualPrice: this.sum.zong,
					userName: this.address.name,
					phone: this.address.phone,
					information: this.address.region,
					address: this.address.address,
					message: this.message,
					list: this.test
				}).then(
					res => {
						if (res.data == 0){
								alert("生成订单失败")
							}else {
								uni.redirectTo({
									url: '/pages/CashierDesk/CashierDesk?id='+res.data+'&zong='+this.sum.zong
								})
							}
					}
				)
				
			},
			addnum: function(t) {


				var sum = 0.0;
				var yunsum = 0.0;
				var youhui = 0.0;

				for (let i = 0; i < t.length; i++) {
					sum += (parseFloat(t[i].price, 10) * parseFloat(t[i].quantity, 10));
					yunsum += parseFloat(t[i].fare, 10);
				}
				var zong = sum + yunsum - youhui
				return {
					sum,
					yunsum,
					youhui,
					zong
				};
			},
			/**
			 * 跳转点击
			 * @param {String} type 跳转类型
			 */
			onSkip(type) {
				switch (type) {
					case 'address':
						uni.navigateTo({
							url: '/pages/AddressList/AddressList',
						})
						break;
				}
			}
		}
	}
</script>

<style scoped lang="scss">
	@import 'ConfirmOrder.scss';
</style>
